package fei.typhoon.engine.utils;

import java.util.ArrayList;

public class FeiArrayList<T> extends ArrayList<T> {

	private static final long serialVersionUID = 1L;

	private int next = 0;

	private int fore = 0;

	public T getNext() {
		T t = null;
		if (size() == 0 || next >= size()) {
			next = 0;
			return t;
		}
		try {
			t = get(next);
		} catch (IndexOutOfBoundsException e) {
			t = null;
		}
		next++;
		return t;
	}

	public T getFore() {
		T t = null;
		if (size() == 0 || fore < 0) {
			fore = 0;
			return t;
		}
		try {
			t = get(fore);
		} catch (IndexOutOfBoundsException e) {
			t = null;
		}
		fore--;
		return t;
	}

	public void reset() {
		resetNext();
		resetFore();
	}

	public void resetNext() {
		next = 0;
	}

	public void resetFore() {
		fore = size() - 1;
	}

	public boolean hasNext() {
		return (size() > 0 && next < size());
	}

	public boolean hasFore() {
		return (size() > 0 && fore >= 0);
	}

	public T getLast() {
		T t = null;
		int count = size();
		if (count > 0) {
			try {
				t = get(count - 1);
			} catch (IndexOutOfBoundsException e) {
				t = null;
			}
		}

		return t;
	}

	public void switchWithIndex(int index1, int index2) {
		if (index1 < size() && index2 < size()) {
			try {
				T t1 = get(index1);
				T t2 = get(index2);
				if (t1 != null && t2 != null) {
					set(index1, t2);
					set(index2, t1);
				}
			} catch (IndexOutOfBoundsException e) {
				e.printStackTrace();
			}
		}
	}
}
